package com.zy.xuanwo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.zy.xuanwo.R;
import com.zy.xuanwo.utils.Constants;
import com.zy.xuanwo.utils.StringUtil;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 自定义标题
 * @author lc
 *
 */
public class TitleView extends android.widget.LinearLayout {
	/**
	 * 标题显示文本
	 */
	private TextView mTitleTextView;
	/**
	 * 标题栏上显示/隐藏左边菜单按钮
	 */
	private ImageView mShowLeftSlidingMenuImageView;
	/**
	 * 显示弹出菜单按钮
	 */
	private ImageView mShowPopMenuImageButton;
	
	private int mLeftButtonVisibility = VISIBLE;
	private int mRightButtonVisibility = VISIBLE;
	//@drawable/title_func
	private int mLeftButtonIcon = R.drawable.back_icon;
	private int mRightButtonIcon = R.drawable.add_more_icon;
	
	private Method mTitleOnChange;
	private Method mLeftButtonOnClickMethod;
	private Method mRightButtonOnClickMethod;
	
	public TitleView(Context context, AttributeSet attrs, int defStyle) {
		 super(context, attrs, defStyle);
		 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleView,defStyle, 0);
			
		 String mTitleOnChangeMethodName = typedArray.getString(R.styleable.TitleView_titleOnChange);
		 String mLeftButtonOnClickMethodName = typedArray.getString(R.styleable.TitleView_leftButtonOnClick);
		 String mRightButtonOnClickMethodName = typedArray.getString(R.styleable.TitleView_rightButtonOnClick);
		 mLeftButtonVisibility = typedArray.getInt(R.styleable.TitleView_leftButtonVisibility, VISIBLE);
		 mRightButtonVisibility = typedArray.getInt(R.styleable.TitleView_rightButtonVisibility, VISIBLE);
		 mLeftButtonIcon = typedArray.getResourceId(R.styleable.TitleView_leftButtonIconc, R.drawable.back_icon);
		 mRightButtonIcon = typedArray.getResourceId(R.styleable.TitleView_rightButtonIconc, R.drawable.add_more_icon);
		 try {
			 if(StringUtil.isNotEmpty(mTitleOnChangeMethodName)){
				//Activity中定义的方法，参数第一个String为新显示标题，第二个string为之前标题
				 mTitleOnChange = context.getClass().getMethod(mTitleOnChangeMethodName, String.class,String.class);
			 }
			 if(StringUtil.isNotEmpty(mLeftButtonOnClickMethodName)){
				 mLeftButtonOnClickMethod = context.getClass().getMethod(mLeftButtonOnClickMethodName, View.class);
			 }
			 if(StringUtil.isNotEmpty(mRightButtonOnClickMethodName)){
				 mRightButtonOnClickMethod = context.getClass().getMethod(mRightButtonOnClickMethodName, View.class);
			 }
			
		 } catch (NoSuchMethodException e) {
			 e.printStackTrace();
			 Log.e(Constants.TAG, e.toString());
		 }
		 
		 initView(context);
		 initListener();
		 String title = typedArray.getString(R.styleable.TitleView_titletext);
		 if(StringUtil.isNotEmpty(title)){
			 setTitle(title);
		 }
		 
		 typedArray.recycle();
	}

	public TitleView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	
	public TitleView(Context context) {
		super(context);
		initView(context);
		initListener();
	}

	protected void initView(Context context){
		LayoutInflater inflater = LayoutInflater.from(context);
		View mView = inflater.inflate(R.layout.title_more_layout, this, false);
		mTitleTextView = (TextView) mView.findViewById(R.id.title_text_view);
		mShowLeftSlidingMenuImageView = (ImageView) mView.findViewById(R.id.left_sliding_menu_iv);
		mShowLeftSlidingMenuImageView.setVisibility(mLeftButtonVisibility);
		mShowLeftSlidingMenuImageView.setImageResource(mLeftButtonIcon);
		mShowPopMenuImageButton = (ImageView) mView.findViewById(R.id.right_sliding_menu_iv);
		mShowPopMenuImageButton.setVisibility(mRightButtonVisibility);
		mShowPopMenuImageButton.setImageResource(mRightButtonIcon);
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		addView(mView, params);
	}
	
	/**
	 * 初始化组件所需的监听
	 */
	protected void initListener() {
		mShowLeftSlidingMenuImageView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mLeftButtonOnClickMethod != null){
					try {
						mLeftButtonOnClickMethod.invoke(getContext(), v);
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						e.printStackTrace();
					}
				}
				
			}
		});
		
		mShowPopMenuImageButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mRightButtonOnClickMethod != null){
					try {
						mRightButtonOnClickMethod.invoke(getContext(), v);
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						e.printStackTrace();
					}
				}
				
			}
		});

	}
	
	/**
	 * 设置显示标题
	 * @param title
	 */
	public void setTitle(String title) {
		if (mTitleOnChange != null) {
			try {
				mTitleOnChange.invoke(getContext(), title, mTitleTextView.getText().toString());
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		mTitleTextView.setText(title);
	}
	
	/**
	 * 设置左边按钮是否可见
	 * @param visibility @see {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
	 */
	public void setLeftButtonVisibility(int visibility){
		if(mShowLeftSlidingMenuImageView.getVisibility() != visibility){
			mShowLeftSlidingMenuImageView.setVisibility(visibility);
		}
	}
	/**
	 * 设置右边按钮是否可见
	 * @param visibility @see {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
	 */
	public void setRightButtonVisibility(int visibility){
		if(mShowPopMenuImageButton.getVisibility() != visibility){
			mShowPopMenuImageButton.setVisibility(visibility);
		}
	}

	/**
	 * 设置标题 左边按钮监听
	 * @param onClickListener
	 */
	public void setLeftButtonOnClickListener(
			OnClickListener onClickListener) {
		mShowLeftSlidingMenuImageView.setOnClickListener(onClickListener);
	}
	/**
	 * 设置标题 右边按钮监听
	 * @param onClickListener
	 */
	public void setRightButtonOnClickListener(
			OnClickListener onClickListener) {
		mShowPopMenuImageButton.setOnClickListener(onClickListener);
	}
	

}
